
clear all
set more off
capture log close
est clear

global figure1 			= 0 // 1: run, 0: not 
global figure2 			= 0 // 1: run, 0: not 
global figure3 			= 0 // 1: run, 0: not 
global figure4 			= 0 // 1: run, 0: not  (Adjust after matlab Gio output)
global figure5 			= 0 // 1: run, 0: not  (Adjust after matlab Gio output) 


*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* Figure 1 *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*

if $figure1 == 1 {

*------------------------------------------------------------------------------*
* Chart fraction heard of Bitcoin *
*------------------------------------------------------------------------------*

use "data/working/working_scpc.dta", replace 

gen year = year(date)
collapse (mean) bitcoin_heardof, by(year) 

gen date = mdy(9,30,year) 
format date %td

tsset date

tsfill

merge 1:m date using "data/working/working_coinmarketcap"
keep if currency == "bitcoin"
keep if date > mdy(7,1,2015) 
keep if date < mdy(12,31,2018) 

replace bitcoin_heardof = bitcoin_heardof*100

sort date
twoway 	(bar bitcoin_heardof date, barwidth(30) color(navy)) /// 
		(line close date, yaxis(2) color(maroon)) , ///
		ylabel(0(10)70) ///
		xlabel(`=d(1jul2015)' `=d(1jul2016)' `=d(1jul2017)' `=d(1jul2018)', format(%tdMon-CCYY)) ///
		ytitle("Fraction heard of Bitcoin (%)") ytitle("Bitcoin price ($)", axis(2)) xtitle("") ///
		legend(label (1 "Awareness") label(2 "Price") ring(0) position(11)) ///
		graphregion(color(white)) bgcolor(white)
graph export "output/figures/figure1a.pdf", replace	


*------------------------------------------------------------------------------*
* Chart expectations *
*------------------------------------------------------------------------------*


use "data/working/working_scpc.dta", replace 

bys prim_key: gen N_i_obs = _N
keep if N_i_obs==4

bys scpc_year: egen awareness = mean(bitcoin_heardof)

gen year = year(date)
gen month = month(date)
gen ym_date = ym(year,month)
format ym_date %tm
tab ym_date

bys prim_key: gen fo_i = _n==1

gen begin 	= bitcoin_expectation_nextyear if year==2015
bys prim_key (begin): replace begin = begin[1] if missing(begin)
gen boom 	= bitcoin_expectation_nextyear if year==2017
bys prim_key (boom): replace boom = boom[1] if missing(boom)
gen burst 	= bitcoin_expectation_nextyear if year==2018
bys prim_key (burst): replace burst = burst[1] if missing(burst)


gen increase 	= (bitcoin_expectation_nextyear==4 | bitcoin_expectation_nextyear==5)
gen decrease 	= (bitcoin_expectation_nextyear==1 | bitcoin_expectation_nextyear==2)
gen same 		= (bitcoin_expectation_nextyear==3)

keep if bitcoin_heardof == 1

collapse (mean) increase decrease same bitcoin_own, by(year) 

gen date = mdy(9,30,year) 
format date %td

tsset date

tsfill

merge 1:m date using "data/working/working_coinmarketcap"
keep if currency == "bitcoin"
keep if date > mdy(7,1,2015) 
keep if date < mdy(12,31,2018) 

replace increase = increase*100

sort date
twoway 	(bar increase date, barwidth(30) color(navy)) /// 
		(line close date, yaxis(2) color(maroon)) , ///
		ylabel(0(5)30) ///
		xlabel(`=d(1jul2015)' `=d(1jul2016)' `=d(1jul2017)' `=d(1jul2018)', format(%tdMon-CCYY)) ///
		ytitle("Fraction expect increase (%)") ytitle("Bitcoin price ($)", axis(2)) xtitle("") ///
		legend(label(1 "Expectations") label(2 "Price") ring(0) position(11)) ///
		graphregion(color(white)) bgcolor(white)
graph export "output/figures/figure1b.pdf", replace	
	

}



*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* Figure 2 *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*

if $figure2 == 1 {


use "data/working/data_platform.dta", replace 

keep if currency_id=="bitcoin"

keep first_purchase
drop if first_purchase == ""

gen  year 	= .
replace year = 2009 if first_purchase == "2009 or earlier"
replace year = 2011 if first_purchase == "2010 - 2011"
replace year = 2013 if first_purchase == "2012 - 2013"
replace year = 2015 if first_purchase == "2014-2015"
replace year = 2017 if first_purchase == "2016-2017"
replace year = 2019 if first_purchase == "2017 and later"

gen const = 1

collapse (count) const, by(year)  // ym_date

gen 	cum = const + const[_n-1]
replace cum = const if missing(cum) // first year
replace cum = cum / cum[_N]

egen 	tot = sum(const)
gen 	new = const / tot

gen date = mdy(12,31,year) 
format date %td

tsset date

tsfill

merge 1:1 date using "data/working/working_blockchain_marketprice" // this one goes back to 2009
keep if _merge==3 | _merge==1
drop _merge

replace new = new*100

twoway 	(bar new date, barwidth(30) color(navy)) /// 
		(tsline marketprice , yaxis(2) color(maroon)) , ///
		ylabel(0(5)40) tlabel(`=td(31dec2009)' `=td(31dec2011)' `=td(31dec2013)' `=td(31dec2015)' `=td(31dec2017)' `=td(31dec2019)', format(%dm-CY)) /// 
		ytitle("First purchase in year (%)") ytitle("Bitcoin price ($)", axis(2)) xtitle("") ///
		legend(label (1 "First purchase") label(2 "Price") ring(0) position(11)) ///
		graphregion(color(white)) bgcolor(white)
graph export "output/figures/figure2.pdf", replace


}


*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* Figure 3 *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*

if $figure3 {

use "data/working/working_coinmarketcap.dta", replace 

rename supply supply_check

replace currency = "bitcoin_cash" if currency == "bitcoin-cash"

rename currency currency_id
sort date currency_id
merge 1:1 date currency_id using "data/working/cryptocurrency", keepusing(supply)
keep if _merge!=2
drop _merge


keep if date >= date("20180101","YMD")
keep if currency_id == "bitcoin" | currency_id == "bitcoin_cash" | currency_id == "litecoin"  | ///
		currency_id == "ethereum" | currency_id == "monero" | currency_id == "zcash"  | ///
		currency_id == "ripple" | currency_id == "dash" 

		
*------------------------------------------------------------------------------*
* Time series *
*------------------------------------------------------------------------------*

bys currency_id	(date): gen tmp = supply[1]
bys currency_id	(tmp): replace tmp = tmp[1] if missing(tmp)
gen supply_norm = 100 * supply/tmp

twoway 	(line supply_norm date if currency=="bitcoin", sort color(navy)) /// lwidth(thick)
		(line supply_norm date if currency=="bitcoin_cash", sort lpattern(dash) color(maroon)) ///
		(line supply_norm date if currency=="dash", sort lpattern(shortdash) ) /// 
		(line supply_norm date if currency=="ethereum", sort lpattern(dash_dot) ) ///
		(line supply_norm date if currency=="litecoin", sort lpattern(dot) ) ///
		(line supply_norm date if currency=="monero", sort lpattern(shortdash) ) /// 
		(line supply_norm date if currency=="zcash", sort lpattern(shortdash_dot) yaxis(2)), ///
		ytitle("Supply (normalized Jan 1st 2018 = 100)") ysc(titlegap(3)) ytitle("", axis(2)) xtitle("") /// 
		legend(order (1 "Bitcoin" 2 "Bitcoin-cash" 3 "Dash" 4 "Ethereum" 5 "Litecoin" 6 "Monero" 7 "Zcash (rx)") row(2) size(small)) ///
		xline(21193, lwidth(5) lc(gs12)) ///
		xline(21373, lwidth(5) lc(gs12)) ///
		ttext(110 21193 "Wave 1", size(vsmall)) ///
		ttext(110 21373 "Wave 2", size(vsmall)) ///
		graphregion(color(white)) bgcolor(white)
graph export "output/figures/figure3b.pdf", replace	


*------------------------------------------------------------------------------*
* Cross-section *
*------------------------------------------------------------------------------*

gen month = month(date)

collapse (mean) supply, by(currency_id month)

keep if month == 1

graph bar supply if currency!="ripple", over(currency)	asyvars ///
		ytitle("Average supply in Jan 2018") ysc(titlegap(3))  ///
		legend(label(1 "Bitcoin") label(2 "Bitcoin-cash")  label(3 "Dash") ///
				label(4 "Ethereum") label(5 "Litecoin") label(6 "Monero") label(7 "Zcash") r(2) size(small)) ///
		graphregion(color(white)) bgcolor(white)
graph export "output/figures/figure3a.pdf", replace	


}



*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* Figure 4 *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*

if $figure4 == 1 {

/*
Take data from excel file "output/tables/table7_*" and compute % changes
*/

* Option 1
replace price = price/1000
gen tolabel = string(change, "%2.0f") + "%" 

* Option 2
twoway 	bar change count if count==1, base(0) barw(0.5) bcolor(navy)   || ///
		bar change count if count==2 , base(0) barw(0.5) bcolor(maroon) || ///
		bar change count if count==3 , base(0) barw(0.5) bcolor(dkgreen) || ///
		bar change count if count==4 , base(0) barw(0.5) bcolor(teal) || ///
		bar change count if count==7, base(0) barw(0.5) bcolor(navy)   || /// 
		bar change count if count==8 , base(0) barw(0.5) bcolor(maroon)  || ///
		bar change count if count==9 , base(0) barw(0.5) bcolor(dkgreen)   || ///
		bar change count if count==10 , base(0) barw(0.5) bcolor(teal)   ///
		legend(order(1 "Number of investors" 2 "Short-term positive beliefs" 3 "Portfolio allocations" 4 "Cryptocurrency Prices") r(2)) ///
		xtitle("") xlabel(none) yline(5, lcolor(black)) ///
		ylabel(0(5)-40, grid) ytitle("Change relative to baseline (%)") ///
		xline(5.5, lcolor(black) lpattern(dash)) ///
		text(1.5 3 "Counterfactual: Beliefs only") ///
		text(1.5 8 "Counterfactual: No entry") ///
		graphregion(color(white)) bgcolor(white)
graph export "output/figures/figure4.pdf", replace	




}



*------------------------------------------------------------------------------**------------------------------------------------------------------------------*
* Figure 5 *
*------------------------------------------------------------------------------**------------------------------------------------------------------------------*

if $figure5 == 1 {

/*
Take data from excel file "output/tables/table8_*" and compute % changes
*/


twoway 	bar change count if count==1 , base(0) barw(0.5) bcolor(navy) || ///
		bar change count if count==2  , base(0) barw(0.5) bcolor(dkgreen) || ///
		bar change count if count==3  , base(0) barw(0.5) bcolor(maroon)  ///
		legend(order(1 "Bitcoin" 2 "Ethereum" 3 "Ripple") r(1)) ///
		xtitle("") xlabel(none) yline(0, lcolor(black))  /// 
		ylabel(-35(5)10, grid) ytitle("Portfolio share change (%)")  ///
		graphregion(color(white)) bgcolor(white)
graph export "output/figures/figure5a.pdf", replace

/*
Take data from excel file "output/tables/table8_*"
*/

twoway 	bar change count if count==1 , base(0) barw(0.5) bcolor(navy) || ///
		bar change count if count==2  , base(0) barw(0.5) bcolor(dkgreen) || ///
		bar change count if count==3  , base(0) barw(0.5) bcolor(maroon)  ///
		legend(order(1 "Bitcoin" 2 "Ethereum" 3 "Ripple") r(1)) ///
		xtitle("") xlabel(none) yline(0, lcolor(black))  /// 
		ylabel(-35(5)10, grid) ytitle("Price change (%)")  ///
		graphregion(color(white)) bgcolor(white)
graph export "output/figures/figure5b.pdf", replace	



}


